package com.gvt.port.service.util;

import java.io.File;
import java.io.FileInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.gvt.port.service.client.security.Passport;

public final class ClientProperties
{

	private final static Properties systemProperties = new Properties();

	static
	{
		try
		{
			final String portClientConfig = System.getProperty("portclient.config");
			
            if(portClientConfig == null)
            {
                systemProperties.load(ClientProperties.class.getResourceAsStream("/client.properties"));
            }
            else
            {
            	systemProperties.load(new FileInputStream(new File(portClientConfig)));
            }
            
            Logger.getLogger(ClientProperties.class).debug("Propriedades carregadas.");
            
        }
		catch (Exception e)
		{
            Logger.getLogger(ClientProperties.class).error("Erro ao carregar properties.", e);
        }
	}

	public static Passport getPassport ()
	{
		try
		{
			final Passport passport = new Passport();
	
			passport.setUserName(systemProperties.getProperty("passport.username"));
	
			passport.setSystem(systemProperties.getProperty("passport.system"));
	
			Logger.getLogger(ClientProperties.class).info("Passport criado com sucesso.");
			Logger.getLogger(ClientProperties.class).debug("Username utilizado :: " + passport.getUserName());
			Logger.getLogger(ClientProperties.class).debug("Password utilizado :: " + passport.getSystem());
			
			return passport;
            
        }
		catch (Throwable e)
		{
            Logger.getLogger(ClientProperties.class).error("Erro ao carregar Passport.", e);
        }
		
		return null;
	}

	public static URL getPortabilidadeESBEndPoint () throws MalformedURLException
	{
		return new URL(systemProperties.getProperty("portabilidadeESB.endpoint"));
	}

	public static URL getCorporateEndPoint () throws MalformedURLException
	{
		return new URL(systemProperties.getProperty("portabilidadeCorporate.endpoint"));
	}

	public static URL getPortabilityEAI () throws MalformedURLException
	{
		return new URL(systemProperties.getProperty("portabilidadeEAI.endpoint"));
	}

	public static URL getPortabilitySisnum () throws MalformedURLException
	{
		return new URL(systemProperties.getProperty("portabilidadeSisnum.endpoint"));
	}

	public static String getReagendarMessages (final String key)
	{
		return systemProperties.getProperty("reagendar.message." + key);
	}
}
